﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infrastructure;
using Picking.Common;
using PickingBusiness;
using Infrastructure.Util;


namespace Picking_Winform
{
    public partial class ManualOperationView : FormBase
    {
        private int currentWorkerCountLine1 = 0;
        private int currentWorkerCountLine2 = 0;
        private int currentWorkerCountLine3 = 0;

        private WorkersAndLightsSetupModel modelWorkersAndLightsLine1 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine1 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine1 = new LightAndDNoSetupModel();

        private WorkersAndLightsSetupModel modelWorkersAndLightsLine2 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine2 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine2 = new LightAndDNoSetupModel();

        private WorkersAndLightsSetupModel modelWorkersAndLightsLine3 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine3 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine3 = new LightAndDNoSetupModel();

        private InterfaceUpdateHandler LightTurnOffHandleLine1;
        private InterfaceUpdateHandler LightTurnOffHandleLine2;
        private InterfaceUpdateHandler LightTurnOffHandleLine3;

        private List<string> lightNosWorker1Line1 = new List<string>();
        private List<string> lightNosWorker2Line1 = new List<string>();
        private List<string> lightNosWorker3Line1 = new List<string>();

        private List<string> lightNosWorker1Line2 = new List<string>();
        private List<string> lightNosWorker2Line2 = new List<string>();
        private List<string> lightNosWorker3Line2 = new List<string>();

        private List<string> lightNosWorker1Line3 = new List<string>();
        private List<string> lightNosWorker2Line3 = new List<string>();
        private List<string> lightNosWorker3Line3 = new List<string>();

        private float lblFontSize1 = 8F;
        private float txtFontSize1 = 8F;
        private float btnFontSize1 = 8F;
        private float lblFontSize2 = 8F;
        private float txtFontSize2 = 8F;
        private float btnFontSize2 = 8F;
        private float lblFontSize3 = 8F;
        private float txtFontSize3 = 8F;
        private float btnFontSize3 = 8F;

        /// <summary>
        /// Constructor
        /// </summary>
        public ManualOperationView()
        {
            InitializeComponent();
            this.Controls.Add(base.txtTitle);
        }

        /// <summary>
        /// Get Form Width
        /// </summary>
        /// <returns></returns>
        protected override int GetFormWidth()
        {
            return 820;
        }

        /// <summary>
        /// Set Form Title
        /// </summary>
        private void SetFormTitle()
        {
            this.Text = "手动作业";
            this.lblLineName1.Text = ConfigInfo.LineName1;
            this.lblLineName2.Text = ConfigInfo.LineName2;
            this.lblLineName3.Text = ConfigInfo.LineName3;
        }

        /// <summary>
        /// Form Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_Shown(object sender, EventArgs e)
        {
            this.SetFormTitle();
            this.TurnOnLEDGreen();
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_Load(object sender, EventArgs e)
        {
            this.GetWorkersAndLightsSetup();
            this.InitOperationArea();

            this.InitSebangoAndDNoSetup();
            this.InitLightAndDNoSetup();

            this.LightTurnOffHandleLine1 = new InterfaceUpdateHandler(HandleTurnedOffLightNo1);
            this.LightTurnOffHandleLine2 = new InterfaceUpdateHandler(HandleTurnedOffLightNo2);
            this.LightTurnOffHandleLine3 = new InterfaceUpdateHandler(HandleTurnedOffLightNo3);

            this.InitAIPlug();
            this.tlpWhole.Dock = DockStyle.Fill;
#if DEBUG
            this.tlpWhole.Dock = DockStyle.Bottom;
            this.cmbSerialPortTest.Visible = true;
            this.btnTest.Visible = true;
            this.btnLoad.Visible = true;
#endif
        }

        /// <summary>
        /// Get Current WorkersAndLightsSetup Data From Database
        /// </summary>
        private void GetWorkersAndLightsSetup()
        {
            this.modelWorkersAndLightsLine1 = Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeA);
            this.currentWorkerCountLine1 = this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows.Count;

            this.modelWorkersAndLightsLine2 = Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeB);
            this.currentWorkerCountLine2 = this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows.Count;

            this.modelWorkersAndLightsLine3 = Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeC);
            this.currentWorkerCountLine3 = this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows.Count;
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            foreach (Control control in this.Controls)
            {
                if (control.Tag != null)
                {
                    this.UpdateFont(control);
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.Tag != null)
                {
                    this.UpdateFont(control);
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeCaption1"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize1, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue1"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize1, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeButton1"))
            {
                this.SetFont(control, "SimSun", this.btnFontSize1, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption2"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize2, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue2"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize2, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeButton2"))
            {
                this.SetFont(control, "SimSun", this.btnFontSize2, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption3"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize3, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue3"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize3, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeButton3"))
            {
                this.SetFont(control, "SimSun", this.btnFontSize3, FontStyle.Regular);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            // Line1
            if (this.currentWorkerCountLine1 <= 0)
            {
                this.tlpMain1.Visible = false;
            }
            else if (this.currentWorkerCountLine1 <= 1)
            {
                this.tlpOperationArea2Line1.Visible = false;
                this.tlpOperationArea3Line1.Visible = false;

                this.tlpMain1.ColumnStyles.Clear();
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));

                // Set Font Size
                this.lblFontSize1 = 12F;
                this.txtFontSize1 = 12F;
                this.btnFontSize1 = 12F;
            }
            else if (this.currentWorkerCountLine1 <= 2)
            {
                this.tlpOperationArea3Line1.Visible = false;

                this.tlpMain1.ColumnStyles.Clear();
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));

                // Set Font Size
                this.lblFontSize1 = 6F;
                this.txtFontSize1 = 6F;
                this.btnFontSize1 = 6F;
            }
            else if (this.currentWorkerCountLine1 <= 3)
            {
                // Set Font Size
                this.lblFontSize1 = 3F;
                this.txtFontSize1 = 3F;
                this.btnFontSize1 = 3F;
            }

            // Line2
            if (this.currentWorkerCountLine2 <= 0)
            {
                this.tlpMain2.Visible = false;
            }
            else if (this.currentWorkerCountLine2 <= 1)
            {
                this.tlpOperationArea2Line2.Visible = false;
                this.tlpOperationArea3Line2.Visible = false;

                this.tlpMain2.ColumnStyles.Clear();
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));

                // Set Font Size
                this.lblFontSize2 = 12F;
                this.txtFontSize2 = 12F;
                this.btnFontSize2 = 12F;
            }
            else if (this.currentWorkerCountLine2 <= 2)
            {
                this.tlpOperationArea3Line2.Visible = false;

                this.tlpMain2.ColumnStyles.Clear();
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));

                // Set Font Size
                this.lblFontSize2 = 6F;
                this.txtFontSize2 = 6F;
                this.btnFontSize2 = 6F;
            }
            else if (this.currentWorkerCountLine2 <= 3)
            {
                // Set Font Size
                this.lblFontSize2 = 3F;
                this.txtFontSize2 = 3F;
                this.btnFontSize2 = 3F;
            }

            // Line3
            if (this.currentWorkerCountLine3 <= 0)
            {
                this.tlpMain3.Visible = false;
            }
            else if (this.currentWorkerCountLine3 <= 1)
            {
                this.tlpOperationArea2Line3.Visible = false;
                this.tlpOperationArea3Line3.Visible = false;

                this.tlpMain3.ColumnStyles.Clear();
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));

                // Set Font Size
                this.lblFontSize3 = 12F;
                this.txtFontSize3 = 12F;
                this.btnFontSize3 = 12F;
            }
            else if (this.currentWorkerCountLine3 <= 2)
            {
                this.tlpOperationArea3Line3.Visible = false;

                this.tlpMain3.ColumnStyles.Clear();
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));

                // Set Font Size
                this.lblFontSize3 = 6F;
                this.txtFontSize3 = 6F;
                this.btnFontSize3 = 6F;
            }
            else if (this.currentWorkerCountLine3 <= 3)
            {
                // Set Font Size
                this.lblFontSize3 = 3F;
                this.txtFontSize3 = 3F;
                this.btnFontSize3 = 3F;
            }

            // Set Font
            this.InitFont();
        }

        /// <summary>
        /// Init Sebango And DNo
        /// Get Sebango And DNo Setup From Database
        /// </summary>
        private void InitSebangoAndDNoSetup()
        {
            this.modelSebangoDNoLine1 = Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeA);
            this.modelSebangoDNoLine2 = Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeB);
            this.modelSebangoDNoLine3 = Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeC);
        }

        /// <summary>
        /// Init DNo
        /// Get Light And DNo Setup From Database
        /// </summary>
        private void InitLightAndDNoSetup()
        {
            this.modelLightDNoLine1 = Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeA);
            this.modelLightDNoLine2 = Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeB);
            this.modelLightDNoLine3 = Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeC);
        }

        /// <summary>
        /// Init AIPlug
        /// </summary>
        private void InitAIPlug()
        {
            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress1))
            {
                axAiPlug1.ConnectSocket(ConfigInfo.AxAiPlugIpAddress1, ConfigInfo.AxAiPlugPort1);

                axAiPlug1.StartPolling(); //watch the data arrival

                axAiPlug1.BinaryMode = true; //set to true

                axAiPlug1.CommandTimeout = 5000; //set to 5000

                this.axAiPlug1.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug1_DataArrival);
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress2))
            {
                axAiPlug2.ConnectSocket(ConfigInfo.AxAiPlugIpAddress2, ConfigInfo.AxAiPlugPort2);

                axAiPlug2.StartPolling(); //watch the data arrival

                axAiPlug2.BinaryMode = true; //set to true

                axAiPlug2.CommandTimeout = 5000; //set to 5000

                this.axAiPlug2.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug2_DataArrival);
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress3))
            {
                axAiPlug3.ConnectSocket(ConfigInfo.AxAiPlugIpAddress3, ConfigInfo.AxAiPlugPort3);

                axAiPlug3.StartPolling(); //watch the data arrival

                axAiPlug3.BinaryMode = true; //set to true

                axAiPlug3.CommandTimeout = 5000; //set to 5000

                this.axAiPlug3.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug3_DataArrival);
            }
        }

        /// <summary>
        /// Display Light No 1
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo1(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine1.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine1.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo1(workerNumber);
                                    int endLightNo = this.GetLightEndNo1(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line1.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line1.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line1.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Display Light No 2
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo2(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine2.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine2.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo2(workerNumber);
                                    int endLightNo = this.GetLightEndNo2(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line2.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line2.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line2.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Display Light No 3
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo3(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine3.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine3.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo3(workerNumber);
                                    int endLightNo = this.GetLightEndNo3(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line3.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line3.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line3.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Get Light Start No 1
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo1(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light Start No 2
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo2(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light Start No 3
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo3(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light End No 1
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo1(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Get Light End No 2
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo2(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Get Light End No 3
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo3(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Turn On Light 1
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight1(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug1.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug1.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On Light 2
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight2(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug2.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug2.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On Light 3
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight3(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug3.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug3.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On LED Yellow
        /// </summary>
        private void TurnOnLEDYellow()
        {
            //this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDYellowLine1.Trim());
            //this.axAiPlug2.SendCommand("T" + ConfigInfo.CmdLEDYellowLine2.Trim());
            //this.axAiPlug3.SendCommand("T" + ConfigInfo.CmdLEDYellowLine3.Trim());
        }

        /// <summary>
        /// Turn On LED Green
        /// </summary>
        private void TurnOnLEDGreen()
        {
            this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDGreenLine1.Trim());
            this.axAiPlug2.SendCommand("T" + ConfigInfo.CmdLEDGreenLine2.Trim());
            this.axAiPlug3.SendCommand("T" + ConfigInfo.CmdLEDGreenLine3.Trim());
        }

        /// <summary>
        /// Turn On LED Red 1
        /// </summary>
        private void TurnOnLEDRed1()
        {
            this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDRedLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Red 2
        /// </summary>
        private void TurnOnLEDRed2()
        {
            this.axAiPlug2.SendCommand("t" + ConfigInfo.CmdLEDRedLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Red 3
        /// </summary>
        private void TurnOnLEDRed3()
        {
            this.axAiPlug3.SendCommand("t" + ConfigInfo.CmdLEDRedLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 1
        /// </summary>
        private void TurnOnLEDBeep1()
        {
            //this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDBeepLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 2
        /// </summary>
        private void TurnOnLEDBeep2()
        {
            //this.axAiPlug2.SendCommand("t" + ConfigInfo.CmdLEDBeepLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 3
        /// </summary>
        private void TurnOnLEDBeep3()
        {
            //this.axAiPlug3.SendCommand("t" + ConfigInfo.CmdLEDBeepLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Red And Beep 1
        /// </summary>
        private void TurnOnLEDRedAndBeep1()
        {
            this.TurnOnLEDRed1();
            //this.TurnOnLEDBeep1();
        }

        /// <summary>
        /// Turn On LED Red And Beep 2
        /// </summary>
        private void TurnOnLEDRedAndBeep2()
        {
            this.TurnOnLEDRed2();
            //this.TurnOnLEDBeep2();
        }

        /// <summary>
        /// Turn On LED Red And Beep 3
        /// </summary>
        private void TurnOnLEDRedAndBeep3()
        {
            this.TurnOnLEDRed3();
            //this.TurnOnLEDBeep3();
        }

        /// <summary>
        /// Get Light No
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightNo(string data)
        {
            string lightNo = string.Empty;
            lightNo = data.Substring(1, 4);

            return lightNo;
        }

        /// <summary>
        /// Get Light Status
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightStatus(string data)
        {
            string status = string.Empty;
            if (data.Length >= 7)
            {
                status = data.Substring(5, 2);
            }
            else if (data.Length >= 6)
            {
                status = data.Substring(5, data.Length - 5);
            }

            return status;
        }

        /// <summary>
        /// Handle Turned Off Light No 1
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo1(string data, string port)
        {
            if (this.currentWorkerCountLine1 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine1 <= 1)
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.lightNosWorker1Line1);
            }
            else if (this.currentWorkerCountLine1 <= 2)
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.lightNosWorker1Line1);
                this.ClearLightNo1(this.ltvLight2Line1, lightNo, lightStatus, this.lightNosWorker2Line1);
            }
            else
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.lightNosWorker1Line1);
                this.ClearLightNo1(this.ltvLight2Line1, lightNo, lightStatus, this.lightNosWorker2Line1);
                this.ClearLightNo1(this.ltvLight3Line1, lightNo, lightStatus, this.lightNosWorker3Line1);
            }
        }

        /// <summary>
        /// Handle Turned Off Light No 2
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo2(string data, string port)
        {
            if (this.currentWorkerCountLine2 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine2 <= 1)
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.lightNosWorker1Line2);
            }
            else if (this.currentWorkerCountLine2 <= 2)
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.lightNosWorker1Line2);
                this.ClearLightNo2(this.ltvLight2Line2, lightNo, lightStatus, this.lightNosWorker2Line2);
            }
            else
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.lightNosWorker1Line2);
                this.ClearLightNo2(this.ltvLight2Line2, lightNo, lightStatus, this.lightNosWorker2Line2);
                this.ClearLightNo2(this.ltvLight3Line2, lightNo, lightStatus, this.lightNosWorker3Line2);
            }
        }


        /// <summary>
        /// Handle Turned Off Light No 3
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo3(string data, string port)
        {
            if (this.currentWorkerCountLine3 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine3 <= 1)
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.lightNosWorker1Line3);
            }
            else if (this.currentWorkerCountLine3 <= 2)
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.lightNosWorker1Line3);
                this.ClearLightNo3(this.ltvLight2Line3, lightNo, lightStatus, this.lightNosWorker2Line3);
            }
            else
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.lightNosWorker1Line3);
                this.ClearLightNo3(this.ltvLight2Line3, lightNo, lightStatus, this.lightNosWorker2Line3);
                this.ClearLightNo3(this.ltvLight3Line3, lightNo, lightStatus, this.lightNosWorker3Line3);
            }
        }

        /// <summary>
        /// Clear Light No 1
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo1(ListView ltvLight, string lightNo, string lightStatus,
                                   List<string> lightNos)
        {
            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo + "异常";
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep1();
                    }
                }
            }

            if (this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();
            }
        }

        /// <summary>
        /// Clear Light No 2
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo2(ListView ltvLight, string lightNo, string lightStatus,
                                   List<string> lightNos)
        {
            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo + "异常";
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep2();
                    }
                }
            }

            if (this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();
            }
        }

        /// <summary>
        /// Clear Light No 3
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo3(ListView ltvLight, string lightNo, string lightStatus,
                                   List<string> lightNos)
        {
            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo + "异常";
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep3();
                    }
                }
            }

            if (this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();
            }
        }

        /// <summary>
        /// Is All Light Turned Off
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <returns></returns>
        private bool IsAllLightTurnedOff(ListView ltvLight)
        {
            bool isAllLightTurnedOff = true;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (ltvLight.Items[i].ForeColor != System.Drawing.Color.Gray)
                {
                    isAllLightTurnedOff = false;
                    break;
                }
            }

            return isAllLightTurnedOff;
        }

        /// <summary>
        /// Do Validation 1
        /// </summary>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private bool DoValidation1(string inputSebangoNo, int workerNumber)
        {
            bool isSebangoExisting = false;
            bool isSebangoHasLightNo = false;
            bool isSebangoHasLightNoUnderWorker = false;
            int sebango = 0;

            if (string.IsNullOrEmpty(inputSebangoNo.Trim()))
            {
                MessageBox.Show("背番号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine1.SebangoAndMachineSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoExisting = true;
                            break;
                        }
                    }
                }

                if (!isSebangoExisting)
                {
                    MessageBox.Show("背番号不存在，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                foreach (DataRow row in this.modelSebangoDNoLine1.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoHasLightNo = true;

                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine1.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo1(workerNumber);
                                    int endLightNo = this.GetLightEndNo1(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        isSebangoHasLightNoUnderWorker = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!isSebangoHasLightNo)
                {
                    MessageBox.Show("该背番号尚没有关联的棚灯号，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                if (!isSebangoHasLightNoUnderWorker)
                {
                    MessageBox.Show("该背番号下的棚灯号不属于本工作人员的操作范围，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("背番号必须是个整型数值，请重新输入!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Do Validation 2
        /// </summary>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private bool DoValidation2(string inputSebangoNo, int workerNumber)
        {
            bool isSebangoExisting = false;
            bool isSebangoHasLightNo = false;
            bool isSebangoHasLightNoUnderWorker = false;
            int sebango = 0;

            if (string.IsNullOrEmpty(inputSebangoNo.Trim()))
            {
                MessageBox.Show("背番号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine2.SebangoAndMachineSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoExisting = true;
                            break;
                        }
                    }
                }

                if (!isSebangoExisting)
                {
                    MessageBox.Show("背番号不存在，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                foreach (DataRow row in this.modelSebangoDNoLine2.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoHasLightNo = true;

                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine2.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo2(workerNumber);
                                    int endLightNo = this.GetLightEndNo2(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        isSebangoHasLightNoUnderWorker = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!isSebangoHasLightNo)
                {
                    MessageBox.Show("该背番号尚没有关联的棚灯号，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                if (!isSebangoHasLightNoUnderWorker)
                {
                    MessageBox.Show("该背番号下的棚灯号不属于本工作人员的操作范围，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("背番号必须是个整型数值，请重新输入!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            return true;
        }

        /// <summary>
        /// Do Validation 3
        /// </summary>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private bool DoValidation3(string inputSebangoNo, int workerNumber)
        {
            bool isSebangoExisting = false;
            bool isSebangoHasLightNo = false;
            bool isSebangoHasLightNoUnderWorker = false;
            int sebango = 0;

            if (string.IsNullOrEmpty(inputSebangoNo.Trim()))
            {
                MessageBox.Show("背番号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine3.SebangoAndMachineSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoExisting = true;
                            break;
                        }
                    }
                }

                if (!isSebangoExisting)
                {
                    MessageBox.Show("背番号不存在，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                foreach (DataRow row in this.modelSebangoDNoLine3.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            isSebangoHasLightNo = true;

                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine3.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo3(workerNumber);
                                    int endLightNo = this.GetLightEndNo3(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        isSebangoHasLightNoUnderWorker = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!isSebangoHasLightNo)
                {
                    MessageBox.Show("该背番号尚没有关联的棚灯号，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }

                if (!isSebangoHasLightNoUnderWorker)
                {
                    MessageBox.Show("该背番号下的棚灯号不属于本工作人员的操作范围，请重新输入!",
                                    "验证",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("背番号必须是个整型数值，请重新输入!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);

                return false;
            }

            return true;
        }

        /// <summary>
        /// btnOK1 Click Line1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK1Line1_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation1(this.txtSebango1Line1.Text.Trim(), 1))
            {
                return;
            }

            this.lightNosWorker1Line1.Clear();
            this.DisplayLightNo1(this.ltvLight1Line1, this.txtSebango1Line1.Text.Trim(), 1);
            this.TurnOnLight1(this.lightNosWorker1Line1);

            if (this.ltvLight1Line1.Items.Count > 0)
            {
                this.btnOK1Line1.Enabled = false;
                this.btnReturn1.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK2 Click Line1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK2Line1_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation1(this.txtSebango2Line1.Text.Trim(), 2))
            {
                return;
            }

            this.lightNosWorker2Line1.Clear();
            this.DisplayLightNo1(this.ltvLight2Line1, this.txtSebango2Line1.Text.Trim(), 2);
            this.TurnOnLight1(this.lightNosWorker2Line1);

            if (this.ltvLight2Line1.Items.Count > 0)
            {
                this.btnOK2Line1.Enabled = false;
                this.btnReturn1.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK3 Click Line1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK3Line1_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation1(this.txtSebango3Line1.Text.Trim(), 3))
            {
                return;
            }

            this.lightNosWorker3Line1.Clear();
            this.DisplayLightNo1(this.ltvLight3Line1, this.txtSebango3Line1.Text.Trim(), 3);
            this.TurnOnLight1(this.lightNosWorker3Line1);

            if (this.ltvLight3Line1.Items.Count > 0)
            {
                this.btnOK3Line1.Enabled = false;
                this.btnReturn1.Enabled = false;
            }
        }


        /// <summary>
        /// btnOK1 Click Line2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK1Line2_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation2(this.txtSebango1Line2.Text.Trim(), 1))
            {
                return;
            }

            this.lightNosWorker1Line2.Clear();
            this.DisplayLightNo2(this.ltvLight1Line2, this.txtSebango1Line2.Text.Trim(), 1);
            this.TurnOnLight2(this.lightNosWorker1Line2);

            if (this.ltvLight1Line2.Items.Count > 0)
            {
                this.btnOK1Line2.Enabled = false;
                this.btnReturn2.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK2 Click Line2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK2Line2_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation2(this.txtSebango2Line2.Text.Trim(), 2))
            {
                return;
            }

            this.lightNosWorker2Line2.Clear();
            this.DisplayLightNo2(this.ltvLight2Line2, this.txtSebango2Line2.Text.Trim(), 2);
            this.TurnOnLight2(this.lightNosWorker2Line2);

            if (this.ltvLight2Line2.Items.Count > 0)
            {
                this.btnOK2Line2.Enabled = false;
                this.btnReturn2.Enabled = false;
            }
        }


        /// <summary>
        /// btnOK3 Click Line2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK3Line2_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation2(this.txtSebango3Line2.Text.Trim(), 3))
            {
                return;
            }

            this.lightNosWorker3Line2.Clear();
            this.DisplayLightNo2(this.ltvLight3Line2, this.txtSebango3Line2.Text.Trim(), 3);
            this.TurnOnLight2(this.lightNosWorker3Line2);

            if (this.ltvLight3Line2.Items.Count > 0)
            {
                this.btnOK3Line2.Enabled = false;
                this.btnReturn2.Enabled = false;
            }
        }


        /// <summary>
        /// btnOK1 Click Line3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK1Line3_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation3(this.txtSebango1Line3.Text.Trim(), 1))
            {
                return;
            }

            this.lightNosWorker1Line3.Clear();
            this.DisplayLightNo3(this.ltvLight1Line3, this.txtSebango1Line3.Text.Trim(), 1);
            this.TurnOnLight3(this.lightNosWorker1Line3);

            if (this.ltvLight1Line3.Items.Count > 0)
            {
                this.btnOK1Line3.Enabled = false;
                this.btnReturn3.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK2 Click Line3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK2Line3_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation3(this.txtSebango2Line3.Text.Trim(), 2))
            {
                return;
            }

            this.lightNosWorker2Line3.Clear();
            this.DisplayLightNo3(this.ltvLight2Line3, this.txtSebango2Line3.Text.Trim(), 2);
            this.TurnOnLight3(this.lightNosWorker2Line3);

            if (this.ltvLight2Line3.Items.Count > 0)
            {
                this.btnOK2Line3.Enabled = false;
                this.btnReturn3.Enabled = false;
            }
        }

        /// <summary>
        /// btnOK3 Click Line3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK3Line3_Click(object sender, EventArgs e)
        {
            if (!this.DoValidation3(this.txtSebango3Line3.Text.Trim(), 3))
            {
                return;
            }

            this.lightNosWorker3Line3.Clear();
            this.DisplayLightNo3(this.ltvLight3Line3, this.txtSebango3Line3.Text.Trim(), 3);
            this.TurnOnLight3(this.lightNosWorker3Line3);

            if (this.ltvLight3Line3.Items.Count > 0)
            {
                this.btnOK3Line3.Enabled = false;
                this.btnReturn3.Enabled = false;
            }
        }



        /// <summary>
        /// Return Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            if (this.axAiPlug1 != null) CloseAllInfoLights(axAiPlug1);
            if (this.axAiPlug2 != null) CloseAllInfoLights(axAiPlug2);
            if (this.axAiPlug3 != null) CloseAllInfoLights(axAiPlug3);
            this.CloseCurrentWindow();
        }

        private void CloseAllInfoLights(AxAiPlugControl.AxAiPlug axAiPlug)
        {
            axAiPlug.SendCommand("T80010");
            axAiPlug.SendCommand("T80020");
            axAiPlug.SendCommand("T80030");
            axAiPlug.SendCommand("T80040");
            axAiPlug.SendCommand("T80050");
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.axAiPlug1.SendCommand("T80010");
            this.axAiPlug1.SendCommand("T80020");
            this.axAiPlug1.SendCommand("T80030");
            this.axAiPlug1.SendCommand("T80040");
            this.axAiPlug1.SendCommand("T80050");
            this.axAiPlug1.StopPolling();
            this.axAiPlug1.Dispose();

            this.axAiPlug2.SendCommand("T80010");
            this.axAiPlug2.SendCommand("T80020");
            this.axAiPlug2.SendCommand("T80030");
            this.axAiPlug2.SendCommand("T80040");
            this.axAiPlug2.SendCommand("T80050");
            this.axAiPlug2.StopPolling();
            this.axAiPlug2.Dispose();

            this.axAiPlug3.SendCommand("T80010");
            this.axAiPlug3.SendCommand("T80020");
            this.axAiPlug3.SendCommand("T80030");
            this.axAiPlug3.SendCommand("T80040");
            this.axAiPlug3.SendCommand("T80050");
            this.axAiPlug3.StopPolling();
            this.axAiPlug3.Dispose();
        }

        /// <summary>
        /// Form Resize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManualOperationView_SizeChanged(object sender, EventArgs e)
        {
            ////this.tlpMain1.Height = this.Height - 150;
        }

        /// <summary>
        /// axAiPlug Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug1_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            if (LightTurnOffHandleLine1 != null)
            {
                this.Invoke(LightTurnOffHandleLine1,
                            new string[] { data, string.Empty });
            }

            if (this.ltvLight1Line1.Items.Count == 0)
            {
                this.btnOK1Line1.Enabled = true;
            }

            if (this.ltvLight2Line1.Items.Count == 0)
            {
                this.btnOK2Line1.Enabled = true;
            }

            if (this.ltvLight3Line1.Items.Count == 0)
            {
                this.btnOK3Line1.Enabled = true;
            }

            if ((this.ltvLight1Line1.Items.Count == 0)
                && (this.ltvLight2Line1.Items.Count == 0)
                && (this.ltvLight3Line1.Items.Count == 0))
            {
                this.btnReturn1.Enabled = true;
            }
        }


        /// <summary>
        /// axAiPlug2 Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug2_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            if (LightTurnOffHandleLine2 != null)
            {
                this.Invoke(LightTurnOffHandleLine2,
                            new string[] { data, string.Empty });
            }

            if (this.ltvLight1Line2.Items.Count == 0)
            {
                this.btnOK1Line2.Enabled = true;
            }

            if (this.ltvLight2Line2.Items.Count == 0)
            {
                this.btnOK2Line2.Enabled = true;
            }

            if (this.ltvLight3Line2.Items.Count == 0)
            {
                this.btnOK3Line2.Enabled = true;
            }

            if ((this.ltvLight1Line2.Items.Count == 0)
                && (this.ltvLight2Line2.Items.Count == 0)
                && (this.ltvLight3Line2.Items.Count == 0))
            {
                this.btnReturn2.Enabled = true;
            }
        }


        /// <summary>
        /// axAiPlug3 Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug3_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            if (LightTurnOffHandleLine3 != null)
            {
                this.Invoke(LightTurnOffHandleLine3,
                            new string[] { data, string.Empty });
            }

            if (this.ltvLight1Line3.Items.Count == 0)
            {
                this.btnOK1Line3.Enabled = true;
            }

            if (this.ltvLight2Line3.Items.Count == 0)
            {
                this.btnOK2Line3.Enabled = true;
            }

            if (this.ltvLight3Line3.Items.Count == 0)
            {
                this.btnOK3Line3.Enabled = true;
            }

            if ((this.ltvLight1Line3.Items.Count == 0)
                && (this.ltvLight2Line3.Items.Count == 0)
                && (this.ltvLight3Line3.Items.Count == 0))
            {
                this.btnReturn3.Enabled = true;
            }
        }


        /// <summary>
        /// Load Data For Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.cmbSerialPortTest.Items.Clear();
            foreach (string item in this.lightNosWorker1Line1)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker2Line1)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker3Line1)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }



            foreach (string item in this.lightNosWorker1Line2)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker2Line2)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker3Line2)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }


            foreach (string item in this.lightNosWorker1Line3)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker2Line3)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }

            foreach (string item in this.lightNosWorker3Line3)
            {
                this.cmbSerialPortTest.Items.Add("t" + item + "00");
            }
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            AxAiPlugControl.__AiPlug_DataArrivalEvent e2
                = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

            this.axAiPlug1_DataArrival(null, e2);


            AxAiPlugControl.__AiPlug_DataArrivalEvent e3
                = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

            this.axAiPlug2_DataArrival(null, e2);


            AxAiPlugControl.__AiPlug_DataArrivalEvent e4
                = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

            this.axAiPlug3_DataArrival(null, e2);
        }
    }
}